package com.yunpeng.dishes.biz.utils;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.dishes.common.constants.ConstantValues;
import com.yunpeng.dishes.common.dtos.business.DishesFoodSaveDTO;
import com.yunpeng.dishes.common.dtos.business.OrderDishesSnapshotDTO;
import com.yunpeng.dishes.common.vo.*;
import com.yunpeng.dishes.dao.model.entity.DishesDO;
import com.yunpeng.dishes.dao.model.entity.FoodIngredientsDO;
import com.yunpeng.dishes.dao.model.entity.MerchantDishesDO;
import com.yunpeng.dishes.dao.model.relation.DishesFoodIngredientsDO;
import com.yunpeng.dishes.dao.model.relation.DishesNutrientsDO;
import com.yunpeng.dishes.dao.model.relation.FoodIngredientsNutrientsDO;
import com.yunpeng.dishes.dao.model.relation.OrderDishesNutrientsDO;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * TransformUtil DO转VO
 *
 * @author : Moore(hemw)
 * @date : 2020-03-26
 */
public class TransformUtil {

    /**
     * 转化食材营养元素表
     *
     * @param foodIngredientsDO
     * @param nutrientsList
     * @return
     */
    public static FoodNutrientsVO transform(FoodIngredientsDO foodIngredientsDO, List<NutrientsVO> nutrientsList) {
        FoodNutrientsVO foodNutrientsVO = new FoodNutrientsVO();
        if (foodIngredientsDO != null) {
            foodNutrientsVO.setId(foodIngredientsDO.getId());
            foodNutrientsVO.setCode(foodIngredientsDO.getCode());
            foodNutrientsVO.setName(foodIngredientsDO.getName());
            foodNutrientsVO.setCategory(foodIngredientsDO.getCategory());
        }
        foodNutrientsVO.setNutrients(nutrientsList);
        return foodNutrientsVO;
    }

    /**
     * 转化食材的营养元素表
     *
     * @param foodIngredientsNutrientsDO
     * @return
     */
    public static NutrientsVO transform(FoodIngredientsNutrientsDO foodIngredientsNutrientsDO) {
        if (foodIngredientsNutrientsDO == null) {
            return new NutrientsVO();
        }
        return getNutrientsVO(foodIngredientsNutrientsDO.getNutrientsCategory(), foodIngredientsNutrientsDO.getNutrientsCode(), foodIngredientsNutrientsDO.getNutrientsName(), foodIngredientsNutrientsDO.getNutrientsUnit(), foodIngredientsNutrientsDO.getNetContent(), foodIngredientsNutrientsDO.getSeq());
    }


    /**
     * 转化菜品营养元素表
     *
     * @param dishesNutrientsDO
     * @return
     */
    public static NutrientsVO transform(DishesNutrientsDO dishesNutrientsDO) {
        if (dishesNutrientsDO == null) {
            return new NutrientsVO();
        }
        return getNutrientsVO(dishesNutrientsDO.getNutrientsCategory(), dishesNutrientsDO.getNutrientsCode(), dishesNutrientsDO.getNutrientsName(), dishesNutrientsDO.getNutrientsUnit(), dishesNutrientsDO.getNetContent(), dishesNutrientsDO.getSeq());
    }

    /**
     * 转化菜品营养元素表
     *
     * @param dishesDO
     * @param dishesFoodVOList
     * @param nutrientsVOList
     * @return
     */
    public static DishesNutrientsVO transform(MerchantDishesDO dishesDO, List<DishesFoodVO> dishesFoodVOList, List<NutrientsVO> nutrientsVOList) {
        DishesNutrientsVO dishesNutrientsVO = new DishesNutrientsVO();
        if (dishesDO != null) {
            dishesNutrientsVO.setId(dishesDO.getId());
            dishesNutrientsVO.setCode(dishesDO.getCode());
            dishesNutrientsVO.setName(dishesDO.getName());
            dishesNutrientsVO.setCategory(dishesDO.getCategory());
            dishesNutrientsVO.setType(dishesDO.getType());
            dishesNutrientsVO.setMerchantId(dishesDO.getMerchantId());
            dishesNutrientsVO.setMerchantDishesId(dishesDO.getMerchantDishesId());
            dishesNutrientsVO.setWeight(dishesDO.getWeight());
        }
        dishesNutrientsVO.setDishesFoods(dishesFoodVOList);
        dishesNutrientsVO.setNutrients(nutrientsVOList);
        return dishesNutrientsVO;
    }

    /**
     * 转化菜品营养元素表
     *
     * @param dishesDO
     * @param dishesFoodVOList
     * @param nutrientsVOList
     * @return
     */
    public static DishesNutrientsVO transform(DishesDO dishesDO, List<DishesFoodVO> dishesFoodVOList, List<NutrientsVO> nutrientsVOList) {
        DishesNutrientsVO dishesNutrientsVO = new DishesNutrientsVO();
        if (dishesDO != null) {
            dishesNutrientsVO.setId(dishesDO.getId());
            dishesNutrientsVO.setCode(dishesDO.getCode());
            dishesNutrientsVO.setName(dishesDO.getName());
            dishesNutrientsVO.setCategory(dishesDO.getCategory());
            dishesNutrientsVO.setType(dishesDO.getType());
            dishesNutrientsVO.setWeight(dishesDO.getWeight());
        }
        dishesNutrientsVO.setDishesFoods(dishesFoodVOList);
        dishesNutrientsVO.setNutrients(nutrientsVOList);
        return dishesNutrientsVO;
    }


    /**
     * 转化菜品关联食材
     *
     * @param dishesFoodIngredientsDO
     * @return
     */
    public static DishesFoodVO transform(DishesFoodIngredientsDO dishesFoodIngredientsDO) {
        DishesFoodVO dishesFoodVO = new DishesFoodVO();
        dishesFoodVO.setCode(dishesFoodIngredientsDO.getFoodIngredientsCode());
        dishesFoodVO.setName(dishesFoodIngredientsDO.getFoodName());
        dishesFoodVO.setCategory(dishesFoodIngredientsDO.getFoodCategory());
        dishesFoodVO.setNetContent(dishesFoodIngredientsDO.getNetContent());
        dishesFoodVO.setFoodProportion(dishesFoodIngredientsDO.getFoodProportion());
        dishesFoodVO.setSeq(dishesFoodIngredientsDO.getSeq());
        return dishesFoodVO;
    }


    /**
     * 组装菜品食材关系数据
     *
     * @param dishesDO
     * @param dishesFoodSaveDto
     * @param foodNutrientsVO
     * @return
     */
    public static DishesFoodIngredientsDO transform(MerchantDishesDO dishesDO, DishesFoodSaveDTO dishesFoodSaveDto, FoodNutrientsVO foodNutrientsVO) {
        DishesFoodIngredientsDO dishesFoodIngredientsDO = new DishesFoodIngredientsDO();
        dishesFoodIngredientsDO.initForNew(dishesDO.getCreator());
        dishesFoodIngredientsDO.setDishesCode(dishesDO.getCode());
        dishesFoodIngredientsDO.setDishesName(dishesDO.getName());
        dishesFoodIngredientsDO.setFoodIngredientsCode(foodNutrientsVO.getCode());
        dishesFoodIngredientsDO.setFoodName(foodNutrientsVO.getName());
        dishesFoodIngredientsDO.setNetContent(dishesFoodSaveDto.getNetContent());
        dishesFoodIngredientsDO.setSeq(dishesFoodSaveDto.getSeq());
        dishesFoodIngredientsDO.setFoodProportion(dishesFoodSaveDto.getProportion());
        dishesFoodIngredientsDO.setFoodCategory(foodNutrientsVO.getCategory());
        return dishesFoodIngredientsDO;
    }

    /**
     * 转化菜品的营养成分表信息
     *
     * @param dishesDO
     * @param dishesFoodIngredientsDO
     * @param waitSaveDishesNutrientsList
     * @param nutrient
     * @return
     */
    public static DishesNutrientsDO transform(MerchantDishesDO dishesDO, DishesFoodIngredientsDO dishesFoodIngredientsDO, Map<String, DishesNutrientsDO> waitSaveDishesNutrientsList, NutrientsVO nutrient) {
        DishesNutrientsDO dishesNutrientsDO = null;
        if (waitSaveDishesNutrientsList.containsKey(nutrient.getCode())) {
            dishesNutrientsDO = waitSaveDishesNutrientsList.get(nutrient.getCode());
        } else {
            dishesNutrientsDO = new DishesNutrientsDO();
            dishesNutrientsDO.initForNew(dishesDO.getCreator());
            dishesNutrientsDO.setDishesCode(dishesDO.getCode());
            dishesNutrientsDO.setDishesName(dishesDO.getName());
            dishesNutrientsDO.setNutrientsCode(nutrient.getCode());
            dishesNutrientsDO.setNutrientsName(nutrient.getName());
            dishesNutrientsDO.setNutrientsCategory(nutrient.getCategory());
            dishesNutrientsDO.setNutrientsUnit(nutrient.getUnit());
            dishesNutrientsDO.setSeq(nutrient.getSeq());
        }
        if (nutrient.getNetContent() != null) {
            BigDecimal currentNetContent = dishesFoodIngredientsDO.getNetContent().multiply(nutrient.getNetContent()).divide(ConstantValues.FOOD_NET_CONTENT, RoundingMode.HALF_DOWN);
            if (dishesNutrientsDO.getNetContent() == null) {
                dishesNutrientsDO.setNetContent(currentNetContent);
            } else {
                dishesNutrientsDO.setNetContent(dishesNutrientsDO.getNetContent().add(currentNetContent).setScale(2, RoundingMode.HALF_DOWN));
            }
        }
        return dishesNutrientsDO;
    }

    /**
     * 转化商户菜品为菜品模板
     *
     * @param user
     * @param merchantDishesDO
     * @return
     */
    public static DishesDO transform(Long user, String code, MerchantDishesDO merchantDishesDO) {
        DishesDO dishesDO = new DishesDO();
        dishesDO.initForNew(user);
        dishesDO.setCode(code);
        dishesDO.setName(merchantDishesDO.getName());
        dishesDO.setCategory(merchantDishesDO.getCategory());
        dishesDO.setType(merchantDishesDO.getType());
        dishesDO.setWeight(merchantDishesDO.getWeight());
        return dishesDO;
    }

    /**
     * 转化订单快照数据
     *
     * @param orderDishesSnapshotDto
     * @param snapshot
     * @return
     */
    public static OrderDishesNutrientsDO transform(OrderDishesSnapshotDTO orderDishesSnapshotDto, JSONObject snapshot) {
        OrderDishesNutrientsDO orderDishesNutrientsDO = new OrderDishesNutrientsDO();
        orderDishesNutrientsDO.initForNew(orderDishesSnapshotDto.getConsumerId());
        orderDishesNutrientsDO.setOrderId(orderDishesSnapshotDto.getOrderId());
        orderDishesNutrientsDO.setOrderCode(orderDishesSnapshotDto.getOrderCode());
        orderDishesNutrientsDO.setMerchantId(orderDishesSnapshotDto.getMerchantId());
        orderDishesNutrientsDO.setConsumer(orderDishesSnapshotDto.getConsumerId());
        orderDishesNutrientsDO.setMealType(orderDishesSnapshotDto.getMealType());
        if (snapshot != null && !snapshot.isEmpty()) {
            orderDishesNutrientsDO.setDishesSnapshots(snapshot.toJSONString());
        }
        orderDishesNutrientsDO.setVersion(new Short("1"));
        return orderDishesNutrientsDO;
    }

    /**
     * 转化订单快照展示数据
     *
     * @param orderDishesNutrientsDO
     * @return
     */
    public static OrderNutrientsVO transform(OrderDishesNutrientsDO orderDishesNutrientsDO) {
        OrderNutrientsVO orderNutrientsVO = new OrderNutrientsVO();
        orderNutrientsVO.setOrderId(orderDishesNutrientsDO.getOrderId());
        orderNutrientsVO.setOrderCode(orderDishesNutrientsDO.getOrderCode());
        orderNutrientsVO.setMerchantId(orderDishesNutrientsDO.getMerchantId());
        orderNutrientsVO.setConsumerId(orderDishesNutrientsDO.getConsumer());
        orderNutrientsVO.setVersion(orderDishesNutrientsDO.getVersion());
        orderNutrientsVO.setSnapshot(JSONObject.parseObject(orderDishesNutrientsDO.getDishesSnapshots()));
        orderNutrientsVO.setMealType(orderDishesNutrientsDO.getMealType());
        return orderNutrientsVO;
    }


    private static NutrientsVO getNutrientsVO(String nutrientsCategory, String nutrientsCode, String nutrientsName, String nutrientsUnit, BigDecimal netContent, Short seq) {
        NutrientsVO nutrientsVO = new NutrientsVO();
        nutrientsVO.setCategory(nutrientsCategory);
        nutrientsVO.setCode(nutrientsCode);
        nutrientsVO.setName(nutrientsName);
        nutrientsVO.setUnit(nutrientsUnit);
        nutrientsVO.setNetContent(netContent);
        nutrientsVO.setSeq(seq);
        return nutrientsVO;
    }
}
